package test.my.com.myapplicationtest.roll_number;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import android.widget.ThemedSpinnerAdapter;

/**
 * Author : Cooper
 * Time : 2017/2/20  11:08
 * Description : 标点符号view 用于解决","、"."这两个符号显示不在同一水平线问题
 */

public class PunctuationTextView extends View {

    private Rect mTextBounds = new Rect();
    private int mTextCenterX;
    private int mTextHeight;
    private Paint mPaint;
    private float mTextSize = 10;
    private int mTextColor = 0xff00ff;
    private String mPunctuation = ",";
    private int mShiftingDownSize ;// 向下偏移量

    public PunctuationTextView(Context context) {
        super(context);
        init();
    }

    public PunctuationTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PunctuationTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setTextAlign(Paint.Align.CENTER);
    }

    public void setPunctuationWithJuHao() {
        this.mPunctuation = ".";
        invalidate();
    }

    public void setPunctuationWithDouHao() {
        this.mPunctuation=",";
        invalidate();
    }

    public void setPunctuationSize(float textSize) {
        this.mTextSize = textSize;
        mPaint.setTextSize(mTextSize);
        measureTextHeight();
        requestLayout();
        invalidate();
    }

    public void setPunctuationColor(int color) {
        this.mTextColor = color;
        mPaint.setColor(mTextColor);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int y = getMeasuredHeight() / 2;
        if (TextUtils.equals(mPunctuation, ",")) {
            mShiftingDownSize = mTextHeight / 2+mTextHeight/3;
        } else if (TextUtils.equals(mPunctuation, ".")) {
            mShiftingDownSize = mTextHeight+mTextHeight*5/2;
        }
        canvas.drawText(mPunctuation + "", mTextCenterX, y + mShiftingDownSize, mPaint);
    }
    private void measureTextHeight() {
        mPaint.getTextBounds(mPunctuation, 0, 1, mTextBounds);
        mTextHeight = mTextBounds.height();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        setMeasuredDimension(width, height);
        mTextCenterX = (getMeasuredWidth() - getPaddingLeft() - getPaddingRight()) / 2;

    }

    private int measureHeight(int measureSpec) {
        int mode = MeasureSpec.getMode(measureSpec);
        int val = MeasureSpec.getSize(measureSpec);
        int result = 0;
        switch (mode) {
            case MeasureSpec.EXACTLY:
                result = val;
                break;
            case MeasureSpec.AT_MOST:
            case MeasureSpec.UNSPECIFIED:
                mPaint.getTextBounds("0", 0, 1, mTextBounds);
                result = mTextBounds.height();
                break;
        }
        result = mode == MeasureSpec.AT_MOST ? Math.min(result, val) : result;
        return result + getPaddingTop() + getPaddingBottom();
    }

    private int measureWidth(int measureSpec) {
        int mode = MeasureSpec.getMode(measureSpec);
        int val = MeasureSpec.getSize(measureSpec);
        int result = 0;
        switch (mode) {
            case MeasureSpec.EXACTLY:
                result = val;
                break;
            case MeasureSpec.AT_MOST:
            case MeasureSpec.UNSPECIFIED:
                mPaint.getTextBounds("0", 0, 1, mTextBounds);
                result = mTextBounds.width();
                break;
        }
        result = mode == MeasureSpec.AT_MOST ? Math.min(result, val) : result;
        return result + getPaddingLeft() + getPaddingRight() - result/2;
    }

}
